<?php

namespace frontend\modules\cms\models;

/**
 * 资讯检索列表模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ArticleSearch extends \common\models\cms\CmsArticle {

    /**
     * 当前用户ID
     */
    public $user_id;

    /**
     * 构建查询
     */
    public function buildQuery() {
        $query = static::findActive('t', static::STATUS_ACTIVE);
        $query->with(['albums', 'video', 'stats']);
        $query->andWhere(['t.channel_id' => $this->channel_id]);

        if ($this->user_id) {
            $query->joinWith([
                'hides h' => function ($query) {
                    $query->andOnCondition(['h.user_id' => $this->user_id])->andWhere(['h.id' => null]);
                }
            ]);
        }

        $query->addOrderBy([
            'updated_at' => SORT_DESC,
        ]);

        return $query;
    }

    /**
     * 文章栏目名称
     */
    public function getCover($format = true) {
        return parent::getCover($format);
    }

    /**
     * 单例返回数据内容格式
     */
    public function fields() {
        return [
            'id',
            'format',
            'title',
            'pictures',
            'duration' => 'splitDuration',
            'picture_nums',
            'stats',
            'publishTime',
            'link',
            'weblink',
        ];
    }

}
